package com.emptyMe.pet.controller;

import com.emptyMe.basic.utils.AjaxResult;
import com.emptyMe.pet.domain.MasterMsg;
import com.emptyMe.pet.domain.Pet;
import com.emptyMe.pet.query.PetQuery;
import com.emptyMe.pet.service.IPetService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(tags = "宠物接口",description = "宠物接口详细api描述")
public class PetController {
    @Autowired
    private IPetService service;

    @PostMapping("/publish")
    @ApiOperation(value = "发布宠物信息",notes = "寻主")
    public AjaxResult getById(@RequestBody MasterMsg mm) {
        try {
            service.publishMessage(mm);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"发布失败");
        }
    }

    /**
     * 添加或更新 用id判断
     * @param :Pet
     * @return
     * RequestBody : 表示获取请求提的json数据
     * */
    /**/
    @PutMapping
    @ApiOperation(value = "宠物添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Pet pet) {
        if (pet.getId() == null) {
            try {
                service.save(pet);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false, "添加失败-" + e.getCause());
            }
        } else {
            try {
                service.update(pet);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false, "修改失败-" + e.getCause());
            }
        }
    }

    @DeleteMapping("/{id}")  // Pet/2
    @ApiOperation(value = "宠物删除",notes = "删除该id下的宠物")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            service.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false, "删除失败--" + e.getCause());
        }
    }


    @DeleteMapping("/delMany")  // Pet/2
    @ApiOperation(value = "宠物批量删除",notes = "批量删除宠物")
    public AjaxResult deleteMany(@RequestBody List<Pet> pets) {
        try {
            service.removeMany(pets);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false, "删除失败--" + e.getCause());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询",notes = "根据id查询宠物")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return AjaxResult.me(service.getById(id));
    }


    // axios 不支持传对象
    @PostMapping
    @ApiOperation(value = "分页＋模糊查询")
    public AjaxResult list(@RequestBody PetQuery query){
        PageInfo<Pet> info = service.searchData(query);
        return AjaxResult.me(info);
    }

}
